unit FormLoginUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ActnList, StdCtrls, IBConnectionBroker, DB, DBTables, Grids,
  DBGrids, WideStrings, FMTBcd, DBClient, Provider, SqlExpr, ExtCtrls, DBCtrls,
  Buttons, Vcl.Imaging.pngimage, Vcl.ComCtrls, System.Actions,Vcl.Consts;

type
  TFormLogin = class(TForm)
    Editusuario: TEdit;
    Label1: TLabel;
    EditSenha: TEdit;
    Label2: TLabel;
    Image1: TImage;
    ActionList1: TActionList;
    Action1: TAction;
    BitBtnLogin: TBitBtn;
    BitBtnCancelar: TBitBtn;
    Label3: TLabel;
    procedure EditSenhaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Action1Execute(Sender: TObject);
    procedure BitBtnCancelarClick(Sender: TObject);
    procedure BitBtnLoginClick(Sender: TObject);
    procedure EditusuarioKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }

    idpermissao : integer;
    NomeUsuario,usuario : String;
    iniciaTela : boolean;
  end;

var
  FormLogin: TFormLogin;

implementation

{$R *.dfm}

uses DMDadosUnt, frmPrincipalUnt;

procedure TFormLogin.Action1Execute(Sender: TObject);
begin
  iniciaTela := false;
  close;
end;

procedure TFormLogin.BitBtnCancelarClick(Sender: TObject);
begin
  iniciaTela := false;
  close;
end;

procedure TFormLogin.BitBtnLoginClick(Sender: TObject);
var
  telaprincipal : TfrmPrincipal;
begin
    DMDados.cdsBuscaUsuario.Close;
    DMDados.cdsBuscaUsuario.Params[0].AsString := Editusuario.Text;
    DMDados.cdsBuscaUsuario.Open;

    if DMDados.cdsBuscaUsuario.IsEmpty = true then
    begin
      ShowMessage('Usuario inexistente!');
      Editusuario.SetFocus;
      exit;
    end;

    if DMDados.cdsBuscaUsuarioSENHA.AsString = EditSenha.Text then
    begin
        Visible := false;

        telaprincipal := TfrmPrincipal.Create(self);

        telaprincipal.nomeUsuario := DMDados.cdsBuscaUsuarioNOME.AsString;
        telaprincipal.usuario := DMDados.cdsBuscaUsuarioUSUARIO.AsString;
        telaprincipal.ShowModal;

        if telaprincipal.logoff then
        begin
         Visible := true;
         Editusuario.Text := '';
         EditSenha.Text := '';
         Editusuario.SetFocus;
        end
        else
        begin
         close;
        end;




    end
    else
    begin

      ShowMessage('Senha incorreta');

    end;
end;

procedure TFormLogin.EditSenhaKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  iniciaTela := false;
  if key = VK_RETURN then
  begin

    BitBtnLogin.Click;

  end;
end;

procedure TFormLogin.EditusuarioKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_RETURN then
  begin
    EditSenha.SetFocus;
  end;
end;

end.
